﻿/*
 * Copyright (C) 2010 Proxy Switch <http://proxyswitch.googlecode.com/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace ProxySwitch
{
    public partial class UpdateCheck : Form
    {
        // declare VersionURL and result
        string VersionURL = "http://proxyswitch.googlecode.com/hg/version.txt";
        string result = null;

        public UpdateCheck()
        {
            InitializeComponent();
        }

        private void UpdateCheck_Load(object sender, EventArgs e)
        {
            // start UpdateCheckThread
            UpdateCheckThread.RunWorkerAsync();

            // hide UpdateCheck form
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void UpdateCheckThread_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                // read the first line in the file to string
                WebClient Client = new WebClient();
                result = Client.DownloadString(VersionURL);

                // updated every version to mark what version client is using
                // if the file on the website doesnt contain the same line
                // then the user is prompted with a update box
                if (result.Contains("ProxySwitch_v1.3"))
                {
                    // do nothing
                }
                else
                {
                    // prompt user to download the update
                    // might remove this to allow more control for admins
                    DialogResult UpdateResult = MessageBox.Show("A update for Proxy Switch avaliable! Would you like to downlo ad the latest version?", "Update Avaliable!",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                    if (UpdateResult == DialogResult.Yes)
                    {
                        // exit application
                        Application.Exit();

                        // downloads page
                        System.Diagnostics.Process.Start("http://proxyswitch.googlecode.com/hg/Update/Update.exe");
                    }
                    else if (UpdateResult == DialogResult.No)
                    {
                        // do nothing
                    }
                }
            }
            // catch the error output to MessageBox
            catch (System.Exception)
            {
                // do nothing
                // this stops the errors when proxy settings not active
            }
        }
    }
}
